home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / MV.C < prev    next >
C/C++ Source or Header  |  1993-06-06  |  4KB  |  153 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville, MI
  3. Date: 06-04-93 (09:08)             Number: 163
  4. From: BOB STOUT                    Refer#: NONE
  5.   To: ALL                           Recvd: NO  
  6. Subj: mv.c                           Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8.   Since someone asked - from the next SNIPPETS:
  9.  
  10. /*
  11. ** mv.c -- move or rename files or directories
  12. ** updated for multiple files, 5 jul 92, rlm
  13. ** placed in the public domain via C_ECHO by the author, Ray McVay
  14. **
  15. ** modified by Bob Stout, 28 Mar 93
  16. ** modified by Bob Stout,  4 Jun 93
  17. **
  18. ** uses file_copy from SNIPPETS file WB_FCOPY.C
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <dos.h>
  25.  
  26. /* For portability, make everything look like MSC 6 */
  27.  
  28. #if defined(__TURBOC__)
  29.  #include <dir.h>
  30.  #define _dos_findfirst(f,a,b) findfirst(f,b,a)
  31.  #define find_t ffblk
  32.  #define _A_SUBDIR FA_DIREC
  33.  #define attrib ff_attrib
  34. #else                   /* assume MSC/QC                                */
  35.  #include <direct.h>
  36. #endif
  37.  
  38. /*
  39. **  Tell 'em they messed up
  40. */
  41.  
  42. void help(char *s)
  43. {
  44.       puts("usage: mv <oldname [...]> <newname|newdir>");
  45.       printf("error: %s\n", s);
  46. }
  47.  
  48. /*
  49. **  Simple directory test
  50. */
  51.  
  52. isdir(char *path)
  53. {
  54.       struct  find_t f;
  55.  
  56.       /* "Raw" drive specs are always directories     */
  57.  
  58.       if (':' == path[1] && '\0' == path[2])
  59.             return 1;
  60.  
  61.       return (_dos_findfirst(path, _A_SUBDIR, &f) == 0 &&
  62.             (f.attrib & _A_SUBDIR));
  63. }
  64.  
  65. /*
  66. **  Use rename or copy and delete
  67. */
  68.  
  69. int mv(char *src, char *dest)
  70. {
  71.       int errcount = 0;
  72.       char buf[FILENAME_MAX];
  73.       const char *generr = "ERROR: mv - couldn't %s %s %s\n";
  74.  
  75.       if (':' == dest[1] && *dest != *getcwd(buf, FILENAME_MAX))
  76.       {
  77.             if (file_copy(src, dest))
  78.             {
  79.                   printf(generr, "move", src, dest);
  80.                   ++errcount;
  81.             }
  82.             else if (unlink(src))
  83.             {
  84.                   printf(generr, "delete", src, "");
  85.                   ++errcount;
  86.             }
  87.       }
  88.       else
  89.       {
  90.             if (rename(src, dest))
  91.             {
  92.                   printf(generr, "rename", src, dest);
  93.                   ++errcount;
  94.             }
  95.       }
  96.       return errcount;
  97. }
  98.  
  99. /*
  100. **  Enter here
  101. */
  102.  
  103. int main(int argc, char **argv)
  104. {
  105.       int src, errcount = 0;
  106.       char target[FILENAME_MAX];
  107.  
  108.       puts("mv 1.3 (4 jun 93) - Ray L. McVay/Bob Stout");
  109.       if (argc < 3)
  110.             help("Not enough parameters");
  111.  
  112.       /*
  113.       **  Handle cases where target is a directory
  114.       */
  115.  
  116.       else if (isdir(argv[argc -1]))
  117.       {
  118.             for (src = 1; src < argc - 1; src++)
  119.             {
  120.                   char termch;
  121.  
  122.                   strcpy(target, argv[argc - 1]);
  123.                   termch = target[strlen(target) - 1];
  124.                   if ('\\' != termch && ':' != termch)
  125.                         strcat(target, "\\");
  126.  
  127.                   if (strrchr(argv[src], '\\'))
  128.                         strcat(target, strrchr(argv[src], '\\') + 1);
  129.                   else if (argv[src][1] == ':')
  130.                         strcat(target, argv[src] + 2);
  131.                   else  strcat(target, argv[src]);
  132.  
  133.                   errcount += mv(argv[src], target);
  134.             }
  135.       }
  136.  
  137.       /*
  138.       **  Nothing left except 2 explicit file names
  139.       */
  140.  
  141.       else if (argc == 3)
  142.             errcount += mv(argv[1], argv[2]);
  143.  
  144.       return errcount;
  145. }
  146.  
  147.  
  148. --- QM v1.00
  149.  * Origin: MicroFirm : Down to the C in chips (1:106/2000.6)
  150. SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
  151. SEEN-BY: 153/752 154/40 77 157/110 159/100 125 430 575 950 203/23 209/209
  152. SEEN-BY: 261/1023 280/1 390/1 396/1 5 15 2270/1 2440/5 3603/20
  153.